using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace MindFair.HomeLeague.Bowling
{
    public class League
    {
        public League(string name)
        {
            this.Name = name;
            _members = new SortedList<string, Bowler>();
            _exposedMembers = new ReadOnlyCollection<Bowler>(_members.Values);
            _activeTournaments = new List<Tournament>();
            _exposedActiveTournaments = new ReadOnlyCollection<Tournament>(_activeTournaments);
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value", "The League Name can not be 'null'.");
                _name = value;
            }
        }

        private SortedList<string, Bowler> _members;
        private ReadOnlyCollection<Bowler> _exposedMembers;
        public ReadOnlyCollection<Bowler> Members
        {
            get { return _exposedMembers; }
        }
        public void AddMember(Bowler newMember)
        {
            if (!_members.ContainsKey(newMember.FullName))
                _members.Add(newMember.FullName, newMember);
        }
        public void RemoveMember(Bowler exMember)
        {
            _members.Remove(exMember.FullName);
        }

        private List<Tournament> _activeTournaments;
        private ReadOnlyCollection<Tournament> _exposedActiveTournaments;
        public ReadOnlyCollection<Tournament> ActiveTournaments
        {
            get { return _exposedActiveTournaments; }
        }

        public Tournament CreateTournament(string name, DateTime startDate)
        {
            Tournament result = new Tournament(name, startDate, this);
            _activeTournaments.Add(result);
            return result;
        }
    }
}
